﻿//wrapper
var chirper = {};
chirper.base = "9er";
chirper.cage_Friend = [];
// constructor for friends
chirper.Friend = function (name, image, base) {
    this.name = name;
    this.image = image;
    this.base = base;
};
chirper.Friend.prototype.isFriend = false;
chirper.urlHelper = function (base) {
    var url = "https://" + base + ".firebaseio.com/"
    for (var i = 1; i < arguments.length; i++) {
        url += arguments[i] + "/"
    }
    return url + ".json";
}
chirper.drawTable_Friend = function () {
    var h = "<p style='text-align: center'><th><h3>Friends</h3></p>";
    for (var i in chirper.cage_Friend) {
        var currentFriend = chirper.cage_Friend[i];
        h += "<p style='text-align: center'><h4>" + currentFriend.name + "</h4></p>";
        h += "<img src='" + currentFriend.image + "' class='img-responsive'/><br/>";
        h += "<p><h4>Base: " + currentFriend.base + "</h4></p>";
        h += "<i class='btn btn-danger fa fa-recycle' onclick='chirper.delete_Friend(" + i + ")'></i>";
        h += "<i class='btn btn-warning fa fa-edit' onclick='chirper.showEdit_Friend(" + i + ")'></i>";
    }
    document.getElementById("friendHolder").innerHTML = h;
};
//chirper.add_Friend = function () {
//    "use strict"
//    var name, image, base;
//    name = document.getElementById("nameF");
//    //image = document.getElementById("image");
//    base = document.getElementById("base");
//    var s = new chirper.Friend(name.value, image.value, base.value)
//    chirper.cage_Friend.push(s);
//    image.value = "";
//    name.value = "";
//    base.value = "";
//    chirper.ajax("POST", chirper.urlHelper(chirper.base, "friends"), s, function () {
//        chirper.read_Friend();
//        //chirper.drawTable_Friend();
//    });

//};
chirper.read_Friend = function () {
        var base;
    base = document.getElementById("base").value;
    chirper.ajax("GET", chirper.urlHelper(base, "profile"), null, function (data) { // get data from friends servers
        console.log(data);
        for (var i in data) {
            var chirp = new chirper.Friend(data[i].name, data[i].image, base); // parameters
            chirp.key = i;
            chirper.cage_Friend.push(chirp);
            chirper.ajax("POST", chirper.urlHelper(chirper.base, "friends"), chirp, function (data) { } ); // push data to your server
            
          
        }
        chirper.drawTable_Friend();
    })
};
//Delete Message
chirper.delete_Friend = function (index) {
    //chirper.ajax("DELETE", chirper.urlHelper(chirper.base, "friends", chirper.cage_Friend[index].key), null, function () {
       // chirper.read_Friend();
   // });
    // setTimeout(function () { chirper.cage_Friend.splice(index, 1); }, 2000);
    chirper.cage_Friend.splice(index, 1);
    chirper.drawTable_Friend();
}
//Show Edit
chirper.showEdit_Friend = function (index) {
    var chirp = chirper.cage_Friend[index];
    //document.getElementById("editTitle").innerHTML = "Message Edit" + chirp.name;
    document.getElementById("editNameF").value = chirp.name;
    document.getElementById("editImage").value = chirp.image;
    document.getElementById("editDomain").value = chirp.base;
    //document.getElementById("isFriend").value = chirp.isFriend.toString();
    document.getElementById("saveChangesBtnF").onclick = function () { chirper.saveEdit_Friend(index); };
    $("#editModalF").modal();

};
// Save Edit
chirper.saveEdit_Friend = function (index) {
    var chirp = chirper.cage_Friend[index];
    chirp.name = document.getElementById("editNameF").value;
    chirp.image = document.getElementById("editImage").value;
    chirp.base = document.getElementById("editDomain").value;
    //if (document.getElementById("isFriend").value === "true") {
    //    chirp.isFriend = true;
    //}
    //else {
    //    delete chirp.isFriend;
    //}
    var s = new chirper.Friend(chirp.name, chirp.image, chirp.bio);
    $("#editModalF").modal("hide");
    chirper.drawTable_Friend();
    chirper.ajax("PATCH", chirper.urlHelper(chirper.base, "friends", chirper.cage_Friend[index].key), s, function () {
        chirper.read_Friend();
    });
};
/**************** AJAX *****************/
chirper.ajax = function (method, url, data, success, error) {
    var request = new XMLHttpRequest();
    request.open(method, url);
    request.onload = function () {
        if (this.status >= 200 && this.status < 400) {
            success(JSON.parse(this.response));
        } else {
            console.log("Error on " + method);
            error();
        }
    };
    request.onerror = function () {
        console.log("Communication error");
        error();
    }
    if (data) {
        request.send(JSON.stringify(data));
    } else {
        request.send();
    }
};
/*****************************************/
//Clear All
chirper.clearAll_Chirp = function () {
    chirper.cage_Friend = [];
    chirper.drawTable_Friend();
}
//Seeder
//chirper.drawTable_Friend();
